package com.dream.dao.impl;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.dream.entity.BannerEntity;
import com.dream.util.DBManager;
import com.dream.util.QueryResult;


public class BannerDaoImpl {
	public ArrayList<BannerEntity> selectAll(Integer page,Integer limit) {
		ArrayList<BannerEntity> aList= new ArrayList<BannerEntity>();
		String sqlString = "select id,banner_img,banner_tiele from banner";
	    if(page!=null&&limit!=null) {
	    	sqlString=sqlString+" limit "+(page-1)*limit+","+limit;
	    }
		
		QueryResult result = DBManager.executeQuery(sqlString);
		if(result!=null) {
			try {
				while(result.rs.next()) {
					aList.add(new BannerEntity(result.rs.getInt("id"),
							result.rs.getString("banner_img"),
							result.rs.getString("banner_tiele")));
				}
			}catch (SQLException e) {
				// TODO: handle exception
			}
		}
		result.close();
		return aList;
	}
	public int bannerUpdate(BannerEntity bEntity) {
		String sqlString = "update banner set banner_img='"+bEntity.getBannerImg()+"',banner_tiele='"+bEntity.getBannerTiele()+"' where id = '"+bEntity.getId()+"'";
		int result = DBManager.executeUpdate(sqlString);
		return result;
	}
	
	public int bannerAdd(BannerEntity bEntity) {
		Date nowdate=new Date();
		SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		String sqlString = "insert into banner(banner_img,banner_tiele,create_time) values ('"+bEntity.getBannerImg()+"','"+bEntity.getBannerTiele()+"','"+simpleDate.format(nowdate)+"')";
		int result = DBManager.executeUpdate(sqlString);
		return result;
	}

	public int bannerDelete(int id) {
		// TODO Auto-generated method stub
		String sqlString = "delete from banner where id = '"+id+"'";
		int result = DBManager.executeUpdate(sqlString);
		return result;
	}
}
